/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package xsystems.GUI;

import java.util.ArrayList;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CombinedDomainXYPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import xsystems.propagationModel.Point;
import xsystems.propagationModel.PropagationModel;

/**
 * Classe utilizada para a plotagem dos gráficos.
 * Define titulo, nome dos eixos, os valores (x,y) a serem plotados.
 * 
 * @author Rafael Tosta
 */
public class Plot {

    private final String titulo = "Propagation Model"; //Ver um nome melhor

    public ChartPanel plotPanel(ArrayList<PropagationModel> lpm) {

        final JFreeChart chart = createCombinedChart(lpm);
        final ChartPanel panel = new ChartPanel(chart, true, true, true, true, true);

        return panel;
    }

    private JFreeChart createCombinedChart(ArrayList<PropagationModel> lpm) {
        final XYDataset data1 = createDataset(lpm);
        final XYItemRenderer renderer1 = new StandardXYItemRenderer();
        final NumberAxis rangeAxis1 = new NumberAxis("Amplitude [dB]");
        final XYPlot subplot = new XYPlot(data1, null, rangeAxis1, renderer1);
        subplot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);

        // parent plot...
        final CombinedDomainXYPlot plot = new CombinedDomainXYPlot(new NumberAxis("Distance [m]"));
        plot.setGap(10.0);

        plot.add(subplot);
        plot.setOrientation(PlotOrientation.VERTICAL);
        
        // return a new chart containing the overlaid plot...
        return new JFreeChart(this.titulo,
                JFreeChart.DEFAULT_TITLE_FONT, plot, true);
    }

    private XYDataset createDataset(ArrayList<PropagationModel> lpm) {
        final XYSeriesCollection collection = new XYSeriesCollection();

        for (int i = 0; i < lpm.size(); i++) {
            PropagationModel pm = lpm.get(i);
            ArrayList<Point> points = pm.getPoints();

            XYSeries series = new XYSeries(pm.getNomeMedodo());
            for (int j = 0; j < points.size(); j++) {
                Point p = points.get(j);
                series.add(p.getX(), p.getY());
            }
            
            collection.addSeries(series);
        }
        return collection;
    }

}
